package threadCase;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: WanqingLiu
 * @Date: 2023/02/04/12:42
 * 用线程池实现多线程网络图片下载
 */
public class WebDownLoad2 implements Runnable{
    private String url;
    private String fileName;
    public WebDownLoad2(String url, String fileName){
        this.url = url;
        this.fileName = fileName;
    }
    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downLoader(url, fileName);
        System.out.println("线程" + Thread.currentThread().getName() + "下载的文件名为" + fileName);
    }

    public static void main(String[] args) {
        // 1. 创建线程池
        ExecutorService service = Executors.newFixedThreadPool(3);
        // 2. 提交任务
        service.submit(new WebDownLoad2("https://img-blog.csdnimg.cn/f45e39eb58bf4ff4bebbcd960740308d.png", "1.png"));
        service.submit(new WebDownLoad2("https://img-blog.csdnimg.cn/1aa82e331f224ebc8950dbfcb07960f2.png", "2.png"));
        service.submit(new WebDownLoad2("https://img-blog.csdnimg.cn/ad9ff5a182e54dda8d7cf73706411d97.png", "3.png"));
        // 3. 关闭线程池
        service.shutdown();
    }
}



